package com.doordu.soa.service.data.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doordu.soa.service.comm.utils.OSSClientFileUtils;
import com.doordu.soa.service.data.dao.ParkingEntryRecordMapper;
import com.doordu.soa.service.data.dao.ParkingImageMapper;
import com.doordu.soa.service.data.entity.ParkingEntryRecord;
import com.doordu.soa.service.data.entity.ParkingImage;
import com.doordu.soa.service.data.entity.example.ParkingEntryRecordExample;
import com.doordu.soa.service.data.entity.example.ParkingImageExample;
import com.doordu.soa.service.data.service.ParkingImageService;
import com.doordu.soa.service.data.vo.ParkingImageVo;
import com.doordu.soa.service.web.utils.FileOperatorUtils;

@Service
public class ParkingImageServiceImpl implements ParkingImageService {

	private final Logger logger = LoggerFactory.getLogger(ParkingImageServiceImpl.class);

	@Autowired
	private ParkingEntryRecordMapper parkingEntryRecordMapper;

	@Autowired
	private ParkingImageMapper parkingImageMapper;

	@Autowired
	private ParkingLotConfig parkingLotConfig;

	public boolean isExistImage(Integer companyId, String dataId, Short imageType) {
		ParkingImageExample example = new ParkingImageExample();
		ParkingImageExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(companyId);
		criteria.andDataIdEqualTo(dataId);
		criteria.andImageTypeEqualTo(imageType);
		int count = parkingImageMapper.countByExample(example);
		return count > 0 ? true : false;
	}

	@Override
	public void syncLightcarImage() {
		ParkingEntryRecordExample example = new ParkingEntryRecordExample();
		ParkingEntryRecordExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(2);
		criteria.andImageUploadFlagEqualTo((short) 0);
		example.setOrderByClause(" record_id asc limit 1000 ");
		List<ParkingEntryRecord> result = parkingEntryRecordMapper.selectByExample(example);
		if (null == result || result.size() == 0) {
			return;
		}

		for (ParkingEntryRecord entity : result) {
			boolean flag = false;
			String enterCapturePic = "";
			if (StringUtils.isNotBlank(entity.getEnterCapturePic())) {
				flag = true;
				boolean isExistImage = isExistImage(entity.getCompanyId(), entity.getDataId(), (short) 2);
				if (!isExistImage) {
					Map map=null;
					try {
						map = OSSClientFileUtils.uploadAliyuncs(parkingLotConfig.getOssAccessKeyId(), parkingLotConfig.getOssAccessSecret(), parkingLotConfig.getOssEndpoint(),
								entity.getEnterCapturePic(), entity.getCompanyId(), parkingLotConfig.getLocalParentPath(), parkingLotConfig.getOssParentPath());
						if (null != map && null != map.get("yourKey") && null != map.get("flag") && map.get("flag").equals("1")) {
							ParkingImage image = setParkingImageInfo(entity, (short) 1, map);
							parkingImageMapper.insertSelective(image);
							entity.setEnterCapturePic(image.getImageUrlRemote());
							enterCapturePic = image.getImageUrlRemote();
							parkingEntryRecordMapper.updateByPrimaryKeySelective(entity);
							try {
								FileOperatorUtils.delFile(map.get("local").toString());
							} catch (Exception e) {

							}
						} else {
							flag = false;
						}
					} catch (Exception e) {
						logger.error("", e);
						ParkingImage image = setParkingImageInfo(entity, (short) 1, map);
						parkingImageMapper.insertSelective(image);
						entity.setEnterCapturePic("");
						enterCapturePic = image.getImageUrlRemote();
						parkingEntryRecordMapper.updateByPrimaryKeySelective(entity);
						flag = true;
					}
				}

			}

			if (StringUtils.isBlank(entity.getLeaveCapturePic())) {
				flag = false;
			} else {
				Map map =null;
				boolean isExistImage = isExistImage(entity.getCompanyId(), entity.getDataId(), (short) 2);
				if (!isExistImage) {
					try {
						map = OSSClientFileUtils.uploadAliyuncs(parkingLotConfig.getOssAccessKeyId(), parkingLotConfig.getOssAccessSecret(), parkingLotConfig.getOssEndpoint(),
								entity.getLeaveCapturePic(), entity.getCompanyId(), parkingLotConfig.getLocalParentPath(), parkingLotConfig.getOssParentPath());
						if (null != map && null != map.get("yourKey") && null != map.get("flag") && map.get("flag").equals("1")) {
							ParkingImage image = setParkingImageInfo(entity, (short) 2, map);
							parkingImageMapper.insertSelective(image);
							entity.setLeaveCapturePic(image.getImageUrlRemote());
							if (StringUtils.isNotBlank(enterCapturePic)) {
								entity.setEnterCapturePic(enterCapturePic);
							}
							parkingEntryRecordMapper.updateByPrimaryKeySelective(entity);
							try {
								FileOperatorUtils.delFile(map.get("local").toString());
							} catch (Exception e) {

							}
						} else {
							flag = false;
						}
					} catch (Exception e) {
						logger.error("", e);
						ParkingImage image = setParkingImageInfo(entity, (short) 1, map);
						parkingImageMapper.insertSelective(image);
						entity.setLeaveCapturePic("");
						parkingEntryRecordMapper.updateByPrimaryKeySelective(entity);
						flag = true;
					}
				}

			}

			if (flag) {
				entity.setImageUploadFlag((short) 1);
				parkingEntryRecordMapper.updateByPrimaryKeySelective(entity);
			}

		}

	}

	private ParkingImage setParkingImageInfo(ParkingEntryRecord entity, Short imageType, Map map) {
		ParkingImage image = new ParkingImage();
		image.setCompanyId(entity.getCompanyId());
		image.setCreateTime(new Date());
		image.setDataId(entity.getDataId());
		image.setImageType(imageType);
		image.setImageUrlSrc(entity.getEnterCapturePic());
		image.setParkingNo(entity.getParkingNo());
		image.setImageUrlRemote(null==map||null==map.get("yourKey")?"":map.get("yourKey").toString());
		return image;
	}

	@Override
	public ParkingImageVo getParkingImage(Integer companyId, String dataId, Short imageType) {
		ParkingImageExample example = new ParkingImageExample();
		ParkingImageExample.Criteria criteria = example.createCriteria();
		criteria.andCompanyIdEqualTo(companyId);
		criteria.andDataIdEqualTo(dataId);
		criteria.andImageTypeEqualTo(imageType);
		List<ParkingImage> list=parkingImageMapper.selectByExample(example);
		if(null==list||list.size()==0){
			return null;
		}
		ParkingImageVo vo=new ParkingImageVo();
		BeanUtils.copyProperties(list.get(0), vo);
		return vo;
	}

}
